if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            user: get_cookie("user"),
            key: get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
                var url = location.href;
                var tiao = true;
                $.each(res.data[0].power_list, function (i, o) {
                    if (url.indexOf(o.url) !== -1) {
                        tiao = false;
                    }
                    var _background_ = url.indexOf(o.url) !== -1 ? 'style="background:#000;"' : "";

                    $(".content_left>ul").append('<a href="' + o.url + '"> <li ' + _background_ + '><span class="glyphicon ' + o.icon + '"></span><p>' + o.names + '</p></li></a>');

                });

                if (tiao) {
                    location.href = res.data[0].power_list[0].url;
                }
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}
$(function () {
    var houtai = {
        lishi_peidan: {
            peidan_status: "",
            start_time: "",
            end_time: "",
            user: ""
        },
        linshi_peidan_add: {
            search: "",
            number: "",
            id: ""
        },
        linshi_peidan_show: {
            id: "",
            is_checked: 1,
            number: ""
        },
        linshi_peidan_order: {
            address_id: "",
            receipt_address_id:null,
            receipt_zizhi_id:null,
            time_data: [],
            discounts_price:""
        },
        linshi_peidan_address: {
            set_address_id: "",
            user_token: "",
            address_data: []
        },
        linshi_coupon:{
          id:"",
          sign:"",
            moeny:""
        },
        linshi_id: null, //临时的id存储 用来判断同一个按钮的操作id
        linshi_address_type:"",//1收货地址   2.收票地址
        linshi_url: "",
        linshi_arrid: [],
        linshi_type: null,//1 设置无效   2 重置订单  3.清除状态  4.删除常用配单商品  5.生成订单
        list_ajax_page: 1,
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        // 检查功能一般检查是否为空
        jiancha: function () {
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint($(".add_goods_body .input_is:visible").eq(i).prev().text() + " 没有填写！");
                    return false;
                }
            }
            return true;
        },
        // 获取配单的数组
        shop_list_ajax: function (page_number) {
            var n = page_number ? page_number : houtai.list_ajax_page = 1;
            $.ajax({
                url: ip + 'index.php?s=admincms/photoorders/showList',
                type: "post",
                data: {
                    status: houtai.lishi_peidan.peidan_status,
                    user: houtai.lishi_peidan.user,
                    start_time: houtai.lishi_peidan.start_time,
                    end_time: houtai.lishi_peidan.end_time,
                    page: n,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        console.log(res);

                    }
                    houtai.table_string(res);

                    $("#listpage").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.list_ajax_page = pageIndex;
                            houtai.shop_list_ajax(pageIndex);
                        }
                    });
                }

            });
        },
        // 把获取的商品数组写入table
        table_string: function (res) {
            console.log(res);
            // 写入table
            var string_head = "<thead> <tr> <th class='text-center'>用户名</th> <th class='text-center'>图片</th> <th class='text-center'>下单时间</th><th class='text-center'>状态</th><th class='text-center'>配置人</th><th class='text-center'>订单号</th><th class='text-center'>操作</th>   </tr> </thead>";
            var string_body = "";
            if (res.resStatus === 1) {
                string_body = "<tr class='text-center'><td colspan='7'>没有数据</td></tr>";
            } else if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    // 判断配单状态
                    var btn_string = "";
                    var status_string = "";
                    var order_number="无";
                    if (o.status === 0) { //未配置
                        btn_string = "<button class='btn btn-xs btn-danger peidan_cancle'>无效</button> <button class='btn btn-success btn-xs  peidan_peizhi'>配置</button>";
                        status_string = "<b class='confirm' style='font-weight: 900;'>待处理<b>";
                    } else if (o.status === 1) { //重置
                        btn_string = "<button class='btn btn-warning btn-xs  peidan_reset'>重置</button>";
                        status_string = "<b class='complete' style='font-weight: 900;'>已配单<b>";
                        order_number='<b>' + o.orderid + '</b>';
                    } else if (o.status === 2) {//重置
                        btn_string = "<button class='btn btn-warning btn-xs  peidan_reset'>重置</button>";
                        status_string = "<b class='down_order' style='font-weight: 900;'>无效<b>";
                    }
                    string_body += '<tr>' +
                        '<td class="peidan_user" style="vertical-align: middle">' + o.user + '</td>' +
                        '<td class="peidan_pid" style="display: none;">' + o.pid + '</td>' +
                        '<td class="peidan_token" style="display: none;">' + o.user_token + '</td>' +
                        '<td class="peidan_img" ><img style="width: 60px;" src="' + o.photo + '" alt=""></td>' +
                        '<td class="peidan_time"  style="vertical-align: middle">' + o.create_time + '</td>' +
                        '<td class="peidan_status" style="vertical-align: middle">' + status_string + '</td>' +
                        '<td class="peidan_peidanren" style="vertical-align: middle">' + o.staffs + '</td>' +
                        '<td class="peidan_order" style="vertical-align: middle">'+order_number+'</td> ' +
                        '<td style="width:100px;vertical-align: middle" class="peidan_control">' + btn_string + '</td> ' +
                        '</tr>';
                });
            }

            $("#peidan_table").empty().append(string_head + string_body);
        },
        //设置订单为无效
        peidan_cancle_ajax: function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/photoorders/setInvalid',
                type: "post",
                data: {
                    pid: houtai.linshi_id,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        $("#modal_wrap").hide();
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        houtai.hint("<span style='color:#0f0;'>设置成功！！！</span>");
                    }
                }

            });
        },
        //设置订单为重置
        peidan_reset_ajax: function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/photoorders/setReset',
                type: "post",
                data: {
                    pid: houtai.linshi_id,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        $("#modal_wrap").hide();
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        houtai.hint("<span style='color:#0f0;'>设置成功！！！</span>");
                    }
                }

            });
        },
        // 获取购物车配单
        peidan_show_ajax: function (page_number) {
            var n = page_number ? page_number : 1;
            $.ajax({
                url: ip + 'index.php?s=admincms/photoorders/setUpOrderShow',
                type: "post",
                data: {
                    pid: houtai.linshi_id,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        console.log(res);

                    }
                    houtai.peidan_show_string(res);

                }

            });
        },
        peidan_show_string: function (res) {
            console.log(res);
            // 写入table
            var string_body = "";
            if (res.resStatus === 1) {
                string_body = "<tr class='text-center'><td colspan='7'>没有数据</td></tr>";
            } else if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    var ischecked = "";
                    if (o.sign === 0) {
                        ischecked = "<input style='width:25px;height:25px;' type='checkbox' class=''/>";
                    } else if (o.sign === 1) {
                        ischecked = "<input style='width:25px;height:25px;' type='checkbox' checked class=''/>";
                    }
                    string_body += '<tr style="height: 50px;">' +
                        '<td class="peidan_show_id">' + o.id + '</td>' +
                        '<td class="peidan_show_name" >' + o.names + '</td>' +
                        '<td class="peidan_show_price" >' + o.price + '</td>' +
                        '<td class="peidan_show_guige" >' + o.create_time + '</td>' +
                        '<td style="width: 160px" class="peidan_show_n">' +
                        '<div class="input-group">' +
                        '<span class="input-group-addon peidan_show_cancle glyphicon glyphicon-minus" style="cursor: pointer"></span>' +
                        '<input type="number" value="' + o.amount + '" style="position:relative;top:1px;" class="form-control">' +
                        '<span class="input-group-addon peidan_show_add glyphicon glyphicon-plus" style="cursor: pointer"></span>' +
                        '</div>' +
                        '</td>' +
                        '<td  class="peidan_show_check" style="width:50px;">' + ischecked + '</td> ' +
                        '</tr>';
                });
            }

            $("#peidan_show_table").empty().append(string_body);
        },
        peidan_show_set: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/photoorders/setOrderEdit",
                type: "post",
                data: {
                    id: houtai.linshi_peidan_show.id,
                    sign: houtai.linshi_peidan_show.is_checked,
                    amount: houtai.linshi_peidan_show.number,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.hint("<span style='color:#0f0;'>成功！！！</span>");
                    }
                }
            });
        },
        peidan_clear_ajax:function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/photoorders/setOffCheckState',
                type: "post",
                data: {
                    user_token:houtai.linshi_peidan_address.user_token,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                      houtai.peidan_show_ajax();
                      $("#modal_wrap").hide();
                        houtai.hint("<span style='color:#0f0;'>清除成功！！！</span>");
                    }

                }

            });
        },
        peidan_remove_ajax:function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/photoorders/delOptGoods',
                type: "post",
                data: {
                     id:houtai.linshi_arrid,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                     $.each(houtai.linshi_arrid,function (a) {
                         $.each($("#peidan_show_table .peidan_show_check>input:checked"),function (i,o) {
                             if($(o).parents("td").siblings(".peidan_show_id").text()===houtai.linshi_arrid[a]){
                                 $(o).parents("tr").remove();
                                 return false;
                             }
                         });
                     });

                     houtai.hint("<span style='color:#0f0;'>删除成功！！！</span>");
                    }
                    $("#modal_wrap").hide();

                }

            });
        },
        order_create_ajax:function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/photoorders/createOrder',
                type: "post",
                data: {
                    siteid:Number(houtai.linshi_peidan_order.address_id),
                    addrId:houtai.linshi_peidan_order.receipt_address_id,
                    invoiceId:houtai.linshi_peidan_order.receipt_zizhi_id,
                    id:houtai.linshi_arrid,
                    pid:houtai.linshi_id,
                    coupon:houtai.linshi_coupon.id,
                    discounts_price:houtai.linshi_peidan_order.discounts_price,
                    delivery_way:$("#peisong_select").val(),
                    send_time:($("#data_select option:selected").text()+" "+$("#time_select option:selected").text()),
                    remark:$.trim($("#order_remark").val()),
                    user_token:houtai.linshi_peidan_address.user_token,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        $("#modal_wrap").hide();
                        houtai.hint("<span style='color:#0f0;'>创建订单成功！！！</span>");
                        $(".set_shangpin").hide();
                        houtai.shop_list_ajax();
                    }

                }

            });
        },
        peidan_show_order_ajax: function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/photoorders/seeOrderItem',
                type: "post",
                data: {
                    pid: houtai.linshi_id,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        console.log(res);
                        $("#peidan_order").show();
                        houtai.peidan_show_order_string(res);
                        // 写入总价
                        $("#tatle").text(Math.float_(res.data.total_price,2));
                        //写入地址
                        if (res.data.address.length !== 0) {
                            $("#address_text").show().find("p").text(res.data.address.Region + " " + res.data.address.ConsigneeAddress);
                            houtai.linshi_peidan_order.address_id = res.data.address.Id;
                        } else {
                            $("#address_text").hide();
                            houtai.linshi_peidan_order.address_id = "";
                        }


                        //写入时间
                        houtai.linshi_peidan_order.time_data = res.data.times;

                        //写入首单满减的价格 创建订单时候用
                        houtai.linshi_peidan_order.discounts_price=res.data.discounts_price;

                        var day_string = "";
                        var time_string = "";
                        $.each(houtai.linshi_peidan_order.time_data, function (i, o) {
                            day_string += '<option value="">' + o.day + '</option>';
                        });
                        $.each(houtai.linshi_peidan_order.time_data[0].times, function (i, o) {
                            time_string += '<option value="">' + houtai.linshi_peidan_order.time_data[0].times[i] + '</option>';
                        });
                        $("#data_select").empty().append(day_string);
                        $("#time_select").empty().append(time_string);
                        if(res.data.discounts_price==="50.00"){
                            houtai.hint("客户满足满1000减50条件 不能使用优惠券");
                            $("#select_coupon_btn").hide();
                            $("#coupon_text").hide();
                        }else{
                            $("#select_coupon_btn").show();
                            if(res.data.coupon){
                                $("#coupon_text").show();
                                houtai.linshi_coupon.id=res.data.coupon.id;
                                $("#coupon_number").text("-"+Math.float_(res.data.coupon.money),2);
                            }else{
                                houtai.linshi_coupon.id=0;
                                $("#coupon_text").hide();
                            }
                        }

                    } else {
                        houtai.hint("请选择商品！！");
                    }

                }

            });
        },
        peidan_show_order_string: function (res) {
            console.log(res);
            // 写入table
            var string_head = "<thead> <tr><th class='text-center'>ID</th><th class='text-center'>商品名称</th><th class='text-center'>商品价格</th><th class='text-center'>商品数量</th> </tr> </thead>";
            var string_body = "";
            if (res.resStatus === 1) {
                string_body = "<tr class='text-center'><td colspan='7'>没有数据</td></tr>";
            } else if (res.resStatus === 0) {
                $.each(res.data.goods_list, function (i, o) {
                    string_body += '<tr style="height: 50px;">' +
                        '<td class="peidan_order_id">' + o.id + '</td>' +
                        '<td class="peidan_order_name" >' + o.names + '</td>' +
                        '<td class="peidan_order_price" >' + o.price + '</td>' +
                        '<td class="peidan_order_amount" >' + o.amount + '</td>' +
                        '</tr>';
                });
            }

            $("#peidan_order_table").empty().append(string_head + string_body);
        },
        // 添加配单展示列表请求
        peidan_add_ajax: function (page_number) {
            var n = page_number ? page_number : 1;
            $.ajax({
                url: ip + 'index.php?s=admincms/photoorders/setUpOrderSearch',
                type: "post",
                data: {
                    names: houtai.linshi_peidan_add.search,
                    pid: houtai.linshi_id,
                    page: n,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        console.log(res);

                    }
                    houtai.peidan_add_string(res);
                    $("#peidan_add_page").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.peidan_add_ajax(pageIndex);
                        }
                    });
                }

            });
        },
        peidan_add_string: function (res) {
            // 写入table
            var string_head = "<thead> <tr> <th class='text-center'>商品ID</th> <th class='text-center'>名称</th> <th class='text-center'>品牌</th><th class='text-center'>规格</th><th class='text-center'>库存量</th><th class='text-center'>数量</th><th class='text-center'>操作</th>   </tr> </thead>";
            var string_body = "";
            if (res.resStatus === 1) {
                string_body = "<tr class='text-center'><td colspan='7'>没有数据</td></tr>";
            } else if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td class="peidan_add_id" style="vertical-align: middle">' + o.goods_id + '</td>' +
                        '<td class="peidan_add_name" style="vertical-align: middle">' + o.names + '</td>' +
                        '<td class="peidan_add_brand" style="vertical-align: middle">' + o.brand + '</td>' +
                        '<td class="peidan_add_specs "  style="vertical-align: middle">' + o.specs + '</td>' +
                        '<td class="peidan_add_stock_amount" style="vertical-align: middle">' + o.stock_amount + '</td>' +
                        '<td class="peidan_add_n" style="vertical-align: middle"><input type="number" class="form-control"></td>' +
                        '<td style="width:100px;vertical-align: middle" class="peidan_add_control"><button class="btn btn-info peidan_add_add">添加</button></td> ' +
                        '</tr>';
                });
            }

            $("#peidan_add_table").empty().append(string_head + string_body);
        },
        //添加配单
        peidan_add_add: function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/photoorders/setUpOrder',
                type: "post",
                data: {
                    pid: houtai.linshi_id,
                    goods_id: houtai.linshi_peidan_add.id,
                    amount: houtai.linshi_peidan_add.number,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        $.each($(".peidan_add_id"), function (i, o) {
                            if ($.trim($(o).text()) === houtai.linshi_peidan_add.id) {
                                $(".peidan_add_id").eq(i).parents("tr").remove();
                            }
                        });
                        houtai.peidan_show_ajax();
                        houtai.hint("<span style='color:#0f0;'>添加成功！！！</span>");
                    }

                }

            });
        },
        //地址显示ajax
        address_list_ajax: function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/address/show',
                type: "post",
                data: {
                    user_token: houtai.linshi_peidan_address.user_token,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        console.log(res);
                        houtai.address_list_string(res.data);
                    }

                }

            });
        },
        address_list_string: function (data) {
            var item_string = '';
            $.each(data, function (i, o) {
                // 判断是哪个地址请求的，才按照哪个类型的显示选中状态
                var id=houtai.linshi_address_type===1?houtai.linshi_peidan_order.address_id:houtai.linshi_peidan_order.receipt_address_id;
                var checked_ = o.id == id ? 'checked="checked"' : "";
                item_string += '<div class="row item_address" style="margin-top: 20px">' +
                    '<div class="col-xs-12">' +
                    '<div class="row item_address_head">' +
                    '<div class="col-xs-3">' + o.names + '</div>' +
                    '<p class="col-xs-9">' + o.phone + '</p>' +
                    '</div>' +
                    '<div class="row item_address_body">' +
                    '<p>' + o.region + ' ' + o.address + '</p>' +
                    '<b style="display: none">' + o.id + '</b>' +
                    '</div>' +
                    '<div class="row item_address_foot">' +
                    '<div class="col-xs-4">' +
                    '<input class="select_address" ' + checked_ + ' type="radio">' +
                    '</div>' +
                    '<div class="col-xs-8">' +
                    '<button class="btn btn-default fr address_set">修改</button>' +
                    '</div>' +
                    '</div>' +
                    '</div>' +
                    '</div>';
            });
            $(".address_wrap").empty().append(item_string);
        },
        //优惠券显示ajax
        coupon_list_ajax: function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/coupon/isUsable',
                type: "post",
                data: {
                    id:houtai.linshi_arrid,
                    user_token: houtai.linshi_peidan_address.user_token
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.errno === 0) {
                        console.log(res);
                        houtai.coupon_list_string(res.data);
                    }else{
                        $(".coupon_wrap").empty();
                    }

                }

            });
        },
        coupon_list_string: function (data) {
            var item_string = '';
            $.each(data.use, function (i, o) {
                var status="";
                var active=o.sign===1?"checked='checked'":"";
                var disable="disabled='disabled'";
                switch (o.status){
                    case 0:
                       status="未激活";
                        break;
                    case 1:
                        disable="";
                        status="未使用";
                        break;
                    case 2:
                       status="已使用";
                        break;
                    case 3:
                       status="已过期";
                        break;
                }

                item_string += '<div class="row item_address" style="margin-top: 20px">' +
                    '<div class="col-xs-12">' +
                    '<div class="row item_address_head">' +
                    '<div class="text-center">' + o.title+ '</div>' +
                    '</div>' +
                    '<div class="row item_address_body">' +
                    '<p style="font-size: 30px">可抵现金<b class="coupon_money">' + o.money+ '</b>元</p>' +
                    '<p style="margin-top: 6px;">' + o.condition+ '</p>' +
                    '<p style="margin-top: 6px;">' + o.explain+ '</p>' +
                    '<p style="margin-top: 30px; text-align: end;">' + o.validTime+ '</p>' +
                    '<b style="display: none" class="coupon_id">' + o.id + '</b>' +
                    '</div>' +
                    '<div class="row item_address_foot">' +
                    '<div class="col-xs-4">' +
                    '<input class="select_coupon" ' + active+ disable+'  type="checkbox">' +
                    '</div>' +
                    '<div class="col-xs-8">' +
                    '<p class=" fr" style="font-size: 20px;">'+status+'</p>' +
                    '</div>' +
                    '</div>' +
                    '</div>' +
                    '</div>';
            });
            $(".coupon_wrap").empty().append(item_string);
        },
        select_coupon_ajax:function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/coupon/couponSign',
                type: "post",
                data: {
                    user_token: houtai.linshi_peidan_address.user_token,
                    coupon:houtai.linshi_coupon.id,
                    sign:houtai.linshi_coupon.sign
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.errno === 0) {
                     $("#coupon_number").text("-"+Math.float_(houtai.linshi_coupon.moeny,2));
                     if(houtai.linshi_coupon.sign===0){
                         $("#coupon_text").hide();
                         houtai.linshi_coupon.id=0;
                     }else{
                         $("#coupon_text").show();
                     }
                    }
                  $("#select_coupon").hide();
                }

            });
        },
        address_add_ajax: function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/address/add',
                type: "post",
                data: {
                    user_token: houtai.linshi_peidan_address.user_token,
                    region: $("#address_city").val() + "-" + $("#address_diqu").val() + "-" + $("#address_scope").val(),
                    names: $("#address_name").val(),
                    phone: $("#address_phone").val(),
                    address: $("#address_detail").val(),
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.address_list_ajax();
                        $("#add_address").hide();
                    }
                }

            });
        },
        address_set_ajax: function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/address/alter',
                type: "post",
                data: {
                    user_token: houtai.linshi_peidan_address.user_token,
                    id: houtai.linshi_peidan_address.set_address_id,
                    region: $("#set_address_city").val() + "-" + $("#set_address_diqu").val() + "-" + $("#set_address_scope").val(),
                    names: $("#set_address_name").val(),
                    phone: $("#set_address_phone").val(),
                    address: $("#set_address_detail").val(),
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        if(houtai.linshi_address_type===1){
                            if (houtai.linshi_peidan_address.set_address_id === houtai.linshi_peidan_order.address_id) {
                                houtai.linshi_peidan_order.address_id = res.data;
                                $("#address_text p").text($("#set_address_city").val() + "-" + $("#set_address_diqu").val() + "-" + $("#set_address_scope").val() + " " + $("#set_address_detail").val());
                            }
                        }else if(houtai.linshi_address_type===2){
                            if(houtai.linshi_peidan_address.set_address_id === houtai.linshi_peidan_order.receipt_address_id){
                                houtai.linshi_peidan_order.receipt_address_id = res.data;
                                $("#receipt_address_text").text($("#set_address_city").val() + "-" + $("#set_address_diqu").val() + "-" + $("#set_address_scope").val() + " " + $("#set_address_detail").val());
                            }
                        }


                        houtai.address_list_ajax();
                        $("#set_address").hide();
                    }
                }

            });
        },
        city_ajax: function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/region/city',
                type: "post",
                async: false,
                data: {
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        console.log(res);
                        // 把数据赋值留着下次用
                        houtai.linshi_peidan_address.address_data = res.data;

                        var city_string = "";
                        var diqi_string = "";
                        var scope_string = "";
                        // 循环第一层
                        $.each(houtai.linshi_peidan_address.address_data, function (i, o) {
                            city_string += '<option value="' + o.province + '">' + o.province + '</option>';
                        });
                        $.each(houtai.linshi_peidan_address.address_data[0].region, function (i, o) {
                            diqi_string += '<option value="' + o.city + '">' + o.city + '</option>';
                        });
                        $.each(houtai.linshi_peidan_address.address_data[0].region[0].county, function (i, o) {
                            scope_string += '<option value="' + houtai.linshi_peidan_address.address_data[0].region[0].county[i] + '">' + houtai.linshi_peidan_address.address_data[0].region[0].county[i] + '</option>';
                        });
                        // 赋值
                        $("#address_city").empty().append(city_string);
                        $("#set_address_city").empty().append(city_string);
                        $("#address_diqu").empty().append(diqi_string);
                        $("#set_address_diqu").empty().append(diqi_string);
                        $("#address_scope").empty().append(scope_string);
                        $("#set_address_scope").empty().append(scope_string);
                    }
                }

            });
        },
        receipt_ajax:function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/invoice/invoiceOrderConfirmPage',
                type: "post",
                data: {
                    user_token: houtai.linshi_peidan_address.user_token,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.errno === 0) {
                       if(res.data.invoice){
                           houtai.linshi_peidan_order.receipt_zizhi_id=res.data.invoice.id;
                           $(".add_receipt_wrap").hide();
                           $(".zizhi_content").show();
                           $(".zizhi_wrap").empty().append(
                           '<p><b>公司名称：</b><span class="receipt_name">'+res.data.invoice.companyName+'</span></p>'+
                           '<p><b>纳税编号：</b><span class="receipt_bianhao">'+res.data.invoice.taxpayerCode+'</span></p>'+
                           '<p><b>公司地址：</b><span class="receipt_address">'+res.data.invoice.regAddr+'</span></p>'+
                           '<p><b>公司电话：</b><span class="receipt_phone">'+res.data.invoice.regPhone+'</span></p>'+
                           '<p><b>公司银行：</b><span class="receipt_bank">'+res.data.invoice.banks+'</span></p>'+
                           '<p><b>公司账户：</b><span class="receipt_zhanghu">'+res.data.invoice.banksAccount+'</span></p>'
                           );
                       }else{
                           $(".zizhi_content").hide();
                           $(".add_receipt_wrap").show();
                       }
                    }
                }

            });
        },
        add_receipt_ajax:function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/invoice/add',
                type: "post",
                data: {
                    user_token: houtai.linshi_peidan_address.user_token,
                    token: get_cookie("key"),
                    companyName:$.trim($("#add_receipt_name").val()),
                    taxpayerCode:$.trim($("#add_receipt_bianhao").val()),
                    regAddr:$.trim($("#add_receipt_address").val()),
                    regPhone:$.trim($("#add_receipt_phone").val()),
                    banks:$.trim($("#add_receipt_bank").val()),
                    banksAccount:$.trim($("#add_receipt_zhanghu").val())
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.errno === 0) {
                     $("#add_receipt").hide();
                        $(".receipt_wrap input:last").click();
                    }
                }

            });
        },
        set_receipt_ajax:function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/invoice/add',
                type: "post",
                data: {
                    user_token: houtai.linshi_peidan_address.user_token,
                    token: get_cookie("key"),
                    companyName:$.trim($("#set_receipt_name").val()),
                    taxpayerCode:$.trim($("#set_receipt_bianhao").val()),
                    regAddr:$.trim($("#set_receipt_address").val()),
                    regPhone:$.trim($("#set_receipt_phone").val()),
                    banks:$.trim($("#set_receipt_bank").val()),
                    banksAccount:$.trim($("#set_receipt_zhanghu").val())
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.errno === 0) {
                     $("#set_receipt").hide();
                        $(".receipt_wrap input:last").click();
                    }
                }

            });
        }
    };


    houtai.shop_list_ajax();


    /*主页 用户发过来的配单照片 */
    $('#input_time').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });

    $("#input_time_btn").bind("click", function () {
        $("#input_time").click();
    });
    // 选择配单状态
    $("#peidan_select").bind("change", function () {
        houtai.lishi_peidan.peidan_status = $.trim($(this).val());
        houtai.lishi_peidan.start_time = "";
        houtai.lishi_peidan.end_time = "";
        houtai.lishi_peidan.user = "";
        houtai.shop_list_ajax();
    });
    // 按时间搜索配单
    $("#search_time").bind("click", function () {
        if ($("#input_time").val() === "") {
            $("#input_time").focus();
            houtai.hint("请输入搜索时间");
        } else {
            if (houtai.click_yes()) {
                // 匹配的正则表达式
                var regexp = /(\d+-\d+-[^-]+)-(.+)/;
                houtai.lishi_peidan.start_time = new Date($("#input_time").val().match(regexp)[1]).getTime() / 1000;
                houtai.lishi_peidan.end_time = new Date($("#input_time").val().match(regexp)[2]).getTime() / 1000;
                houtai.lishi_peidan.user = "";
                houtai.shop_list_ajax();
            }
        }

    });
    // 点击配单
    $("#peidan_table").on("click", ".peidan_peizhi", function () {
        $("#peidan_show").show();
        houtai.linshi_url = $(this).parents("td").siblings(".peidan_img").find("img").attr("src").replace(/_min_/, "_max_");
        houtai.linshi_id = $.trim($(this).parents("td").siblings(".peidan_pid").text());
        houtai.linshi_peidan_address.user_token = $.trim($(this).parents("td").siblings(".peidan_token").text());
        $("#user_id").text($.trim($(this).parents("td").siblings(".peidan_user").text()));
        houtai.peidan_show_ajax();
    });
    // 无效订单操作
    $("#peidan_table").on("click", ".peidan_cancle", function () {
        houtai.linshi_id = $.trim($(this).parents("td").siblings(".peidan_pid").text());
        houtai.linshi_type = 1;
        $("#modal_wrap").show();
        $(".modal-body").empty().append("你确定要设置为无效订单吗？");
    });
    // 重置订单操作
    $("#peidan_table").on("click", ".peidan_reset", function () {
        houtai.linshi_id = $.trim($(this).parents("td").siblings(".peidan_pid").text());
        houtai.linshi_type = 2;
        $("#modal_wrap").show();
        $(".modal-body").empty().append("你确定要重置订单吗？");
    });
    //点击进入订单页详情
    $("#peidan_table").on("click",".peidan_order>b",function () {
    location.href="order_admin.html?"+$(this).text();
    });


    /*配单购物车 里面基本是客户常用的商品*/
    // 点击取消配单展示
    $("#peidan_show .close").bind("click", function () {
        $("#peidan_show").hide();
    });
    //关键字变色input
    $("#replace_input").bind("input", function () {
        var i = 1;
        var val = $.trim($(this).val());
        if (val === "") {
            return false;
        } else {
            var reg_val=val.replace(/([*()+-/.?])/g,"\\$1");
            reg_val = new RegExp(reg_val, "gi");
            $.each($(".peidan_show_name"), function () {
                var that = $(this);
                $(this).html($(this).text().replace(reg_val, function ($1) {
                    if (1 === i++) {
                        $("#scroll_wrap").scrollTop(that.position().top);
                    }
                    return "<span style='color:#f10'>" + $1 + "</span>";

                }));
            });
        }
    });
    // 点击放大图片
    $(".big_img_btn").bind("click", function () {
        $("#big_img").attr("src","images/loading.gif");
        var img=new Image();
        img.src=houtai.linshi_url;
        $(img).bind("load",function () {
            $("#big_img").attr("src", houtai.linshi_url);
            var imgH = $(this)[0].naturalHeight;
            var imgW = $(this)[0].naturalWidth;
            var windW = $(window).width();
            var windH = $(window).height();

            if ((((imgW / imgH) > (windW / windH)) && (imgW < windW)) || (((imgW / imgH) <= (windW / windH)) && (imgH < windH))) {
                $("#big_img").css({
                    left: (windW - imgW) / 2,
                    top: (windH - imgH) / 2,
                    width: imgW,
                    height: imgH
                });
            } else if (((imgW / imgH) > (windW / windH)) && (imgW >= windW)) {
                $("#big_img").css({
                    width: windW,
                    height: windW * (imgH / imgW),
                    top: (windH - (windW * (imgH / imgW))) / 2,
                    left: 0
                });
            } else if (((imgW / imgH) <= (windW / windH)) && (imgH >= windH)) {
                $("#big_img").css({
                    width: windH * (imgW / imgH),
                    height: windH,
                    left: (windW - (windH * (imgW / imgH))) / 2,
                    top: 0
                });
            }
        });

        $("#big_img_wrap").fadeIn();


    });
    //图片加载完成时事件
    $("#big_img").bind("load", function () {
        var imgH = $(this)[0].naturalHeight;
        var imgW = $(this)[0].naturalWidth;
        var windW = $(window).width();
        var windH = $(window).height();

        if ((((imgW / imgH) > (windW / windH)) && (imgW < windW)) || (((imgW / imgH) <= (windW / windH)) && (imgH < windH))) {
            $("#big_img").css({
                left: (windW - imgW) / 2,
                top: (windH - imgH) / 2,
                width: imgW,
                height: imgH
            });
        } else if (((imgW / imgH) > (windW / windH)) && (imgW >= windW)) {
            $("#big_img").css({
                width: windW,
                height: windW * (imgH / imgW),
                top: (windH - (windW * (imgH / imgW))) / 2,
                left: 0
            });
        } else if (((imgW / imgH) <= (windW / windH)) && (imgH >= windH)) {
            $("#big_img").css({
                width: windH * (imgW / imgH),
                height: windH,
                left: (windW - (windH * (imgW / imgH))) / 2,
                top: 0
            });
        }
    });
    // 点击大图消失
    $("#big_img_wrap").bind("click", function () {
        $(this).fadeOut();
    });
    // 添加按钮
    $("#peidan_shop_add").bind("click", function () {
        $("#peidan_add").fadeIn();
        $("#search_input").focus();
    });
    // 加数量按钮
    $("#peidan_show_table").on("click", ".peidan_show_add", function (e) {
        houtai.linshi_peidan_show.id = $(this).parents("td").siblings(".peidan_show_id").text();
        var val = $(this).siblings("input").val();
        $(this).siblings("input").val(+val + 1);
        if (Number($(this).siblings("input").val()) > 9999) {
            $(this).siblings("input").val(9999);
            houtai.hint("不能大于9999！！！");
        } else {
            houtai.linshi_peidan_show.is_checked = 1;
            houtai.linshi_peidan_show.number = $(this).siblings("input").val();
            $(this).parents("td").siblings(".peidan_show_check").find("input").prop("checked", true);
            houtai.peidan_show_set();
        }
        e.preventDefault();
    });
    // 减数量按钮
    $("#peidan_show_table").on("click", ".peidan_show_cancle", function (e) {
        var val = $(this).siblings("input").val();
        $(this).siblings("input").val(val - 1);
        houtai.linshi_peidan_show.id = $(this).parents("td").siblings(".peidan_show_id").text();
        if (Number($(this).siblings("input").val()) < 1) {
            $(this).siblings("input").val(1);
            houtai.hint("不能小于1 ");
        } else {
            houtai.linshi_peidan_show.is_checked = 1;
            houtai.linshi_peidan_show.number = $(this).siblings("input").val();
            $(this).parents("td").siblings(".peidan_show_check").find("input").prop("checked", true);
            houtai.peidan_show_set();
        }
        e.preventDefault();
    });
    // 监视input改变事件
    $("#peidan_show_table").on("input", ".peidan_show_n input", function () {
        houtai.linshi_peidan_show.id = $(this).parents("td").siblings(".peidan_show_id").text();
        if ($(this).val() === "") {
            houtai.hint("不能为空修改不成功");
            return;
        }
        if ($(this).val() % 1 !== 0) {
            $(this).val(parseInt($(this).val()));
        } else if ($(this).val() < 0) {
            $(this).val(Math.abs(parseInt($(this).val())));
        } else if ($(this).val() === "0" || ($(this).val() === "-0")) {
            $(this).val(1);
        } else if ($(this).val() > 9999) {
            $(this).val(9999);
        }
        $(this).parents("td").siblings(".peidan_show_check").find("input").prop("checked", true);
        houtai.linshi_peidan_show.is_checked = 1;
        houtai.linshi_peidan_show.number = $(this).val();
        houtai.peidan_show_set();
    });
    //点击选择事件
    $("#peidan_show_table").on("click", ".peidan_show_check>input", function (e) {
        houtai.linshi_peidan_show.id = $(this).parents("td").siblings(".peidan_show_id").text();
        if ($(this).parents("td").siblings(".peidan_show_n").find("input").val() === "") {
            houtai.hint("数据不能为空");
            $(this).parents("td").siblings(".peidan_show_n").find("input").focus();
            e.preventDefault();
            return;
        }
        if ($(this).prop("checked")) {
            houtai.linshi_peidan_show.is_checked = 1;
            houtai.linshi_peidan_show.number = $(this).parents("td").siblings(".peidan_show_n").find("input").val();
            houtai.peidan_show_set();
        } else {
            houtai.linshi_peidan_show.is_checked = 0;
            houtai.linshi_peidan_show.number = $(this).parents("td").siblings(".peidan_show_n").find("input").val();
            houtai.peidan_show_set();
        }
    });
    //清除状态
    $("#peidan_clear").bind("click", function () {
        houtai.linshi_type = 3;
        $("#modal_wrap").show();
        $(".modal-body").empty().append("你确定要清除配单状态吗？");
    });
    //删除商品
    $("#peidan_remove_btn").bind("click",function () {
        houtai.linshi_arrid = [];
       houtai.linshi_type=4;
       var remove_string="";
       $.each($("#peidan_show_table .peidan_show_check>input:checked"),function (i,o) {
          houtai.linshi_arrid.push($(o).parents("td").siblings(".peidan_show_id").text());
          remove_string+='<span style="color:#f10">'+(i===0?" ":"、")+$(o).parents("td").siblings(".peidan_show_name").text()+'</span>';
       });
       if(remove_string===""){
           houtai.hint("请选择删除的商品！！");
       }else{
           $("#modal_wrap").show();
           $(".modal-body").empty().append("你确定要删除"+remove_string+"吗？");
       }

    });
    // 生成订单按钮
    $("#peidan_preview_create").bind("click", function () {
        houtai.linshi_peidan_order.receipt_address_id=null;
        houtai.linshi_peidan_order.receipt_zizhi_id=null;
        $("#receipt_address_text").hide();
        $(".receipt_wrap input:first").click();
        houtai.peidan_show_order_ajax();
    });


    /* 添加商品   */
    // 关闭配单添加
    $("#close_peidan_add").bind("click", function () {
        $("#peidan_add").hide();
    });
    // 搜索要添加的商品
    $("#search_input").bind("input", function () {
        if ($.trim($(this).val()) === "") {
            return;
        } else {
            houtai.linshi_peidan_add.search = $.trim($(this).val());
            houtai.peidan_add_ajax();
        }
    });
    $("#peidan_add_table").on("click", ".peidan_add_add", function () {
        houtai.linshi_peidan_add.id = $.trim($(this).parents("td").siblings(".peidan_add_id").text());
        var val = $.trim($(this).parents("td").siblings(".peidan_add_n").find("input").val());
        if (val === "") {
            $(this).parents("td").siblings(".peidan_add_n").find("input").focus();
        } else {
            if (val > 0 && val % 1 === 0 && val < 10000) {
                houtai.linshi_peidan_add.number = val;
                houtai.peidan_add_add();
            } else {
                houtai.hint("请输入整数 并且 >0 并且不能大于9999");
            }
        }
    });


    /* 订单生产页*/
    $("#close_peidan_order").bind("click", function () {
        $("#peidan_order").hide();
    });
    //选择是否需要发票
    $(".receipt_wrap :radio").bind("click",function () {
       if($(this).val()==="1"){
           $(".receipt_content").show();
           houtai.receipt_ajax();
       }else{
           $(".receipt_content").hide();
       }
    });
    // 修改发票资质按钮
    $("#set_receipt_btn").bind("click",function () {
        $("#set_receipt_name").val($(".receipt_name").text());
        $("#set_receipt_address").val($(".receipt_address").text());
        $("#set_receipt_zhanghu").val(Number($(".receipt_zhanghu").text()));
        $("#set_receipt_bank").val($(".receipt_bank").text());
        $("#set_receipt_phone").val(Number($(".receipt_phone").text()));
        $("#set_receipt_bianhao").val(Number($(".receipt_bianhao").text()));
       $("#set_receipt").show();
    });
    // 添加发票资质按钮
    $("#add_receipt_btn").bind("click",function () {
       $("#add_receipt").show();
    });
    // 收票地址选择
    $("#select_receipt_address").bind("click",function () {
        houtai.linshi_address_type=2;
        houtai.address_list_ajax();
        houtai.city_ajax();
        $("#select_address").show();
    });
    //收货地址选择
    $("#select_address_btn").bind("click", function () {
        houtai.linshi_address_type=1;
        houtai.address_list_ajax();
        houtai.city_ajax();
        $("#select_address").show();
    });
    $("#select_coupon_btn").bind("click", function () {
        houtai.linshi_arrid=[];
        $.each($("#peidan_order_table .peidan_order_id"),function (i,o) {
            houtai.linshi_arrid.push($(o).text());
        });
        houtai.coupon_list_ajax();
        $("#select_coupon").show();
    });
    $("#data_select").bind("change", function () {
        console.log(houtai.linshi_peidan_order.time_data[0].day);
        console.log($(this).find(":selected").text());
        var that = this;
        $.each(houtai.linshi_peidan_order.time_data, function (i, o) {
            if (houtai.linshi_peidan_order.time_data[i].day === $(that).find(":selected").text()) {
                var time_string = "";
                $.each(houtai.linshi_peidan_order.time_data[i].times, function (index, o) {
                    time_string += '<option value="">' + houtai.linshi_peidan_order.time_data[i].times[index] + '</option>';
                });
                $("#time_select").empty().append(time_string);
                return false;
            }
        });
    });
    // 生成订单
    $("#peidan_order_create").bind("click",function () {
        houtai.linshi_type=5;
        houtai.linshi_arrid=[];
       if(houtai.linshi_peidan_order.address_id===""){
          houtai.hint("没有填写地址");
          $("#select_address_btn").click();
       }else if($(".receipt_wrap input:checked").val()==="1"){
            if(houtai.linshi_peidan_order.receipt_zizhi_id===null){
                houtai.hint("您还没有填写发票资质");
            }else if(houtai.linshi_peidan_order.receipt_address_id===null){
                houtai.hint("您还没有选择收票人地址");
                $("#select_receipt_address").click();
            }else{
                $.each($("#peidan_order_table .peidan_order_id"),function (i,o) {
                    houtai.linshi_arrid.push($(o).text());
                });
                $(".modal-body").empty().append("你确定要生成订单吗？");
                $("#modal_wrap").show();
            }
       }else{
           $.each($("#peidan_order_table .peidan_order_id"),function (i,o) {
               houtai.linshi_arrid.push($(o).text());
           });
           $(".modal-body").empty().append("你确定要生成订单吗？");
           $("#modal_wrap").show();
       }
    });





    /* 选择地址*/
    $("#close_select_address").bind("click", function () {
        $("#select_address").hide();
    });
    $("#add_address_btn").bind("click", function () {
        $("#add_address").show();
    });
    $("#select_address").on("click", ".address_set", function () {
        houtai.linshi_peidan_address.set_address_id = $(this).parents(".item_address").find(".item_address_body>b").text();
        $("#set_address_name").val($(this).parents(".item_address").find(".item_address_head>div").text());
        $("#set_address_phone").val(Number($(this).parents(".item_address").find(".item_address_head>p").text()));
        $("#set_address_detail").val($(this).parents(".item_address").find(".item_address_body>p").text().match(/(.+)-(.+)-([^\s]+)\s(.*)/)[4]);
        var that = this;
        var city = $(that).parents(".item_address").find(".item_address_body>p").text().match(/(.+)-(.+)-(.+)\s(.*)/)[1];
        var diqu = $(that).parents(".item_address").find(".item_address_body>p").text().match(/(.+)-(.+)-(.+)\s(.*)/)[2];
        var address_data = houtai.linshi_peidan_address.address_data;
        var diqu_string = "";
        var scope_string = "";
        $.each(address_data, function (i, o) {
            if (o.province === city) {
                diqu_string = "";
                $.each(o.region, function (i, o) {
                    diqu_string += '<option value="' + o.city + '">' + o.city + '</option>';

                    if (o.city === diqu) {
                        scope_string = "";
                        $.each(o.county, function (i) {
                            scope_string += '<option value="' + o.county[i] + '">' + o.county[i] + '</option>';
                        });
                    }
                });
            }
        });
        $("#set_address_diqu").empty().append(diqu_string);
        $("#set_address_scope").empty().append(scope_string);

        $("#set_address_city").val($(this).parents(".item_address").find(".item_address_body>p").text().match(/(.+)-(.+)-(.+)\s(.*)/)[1]);
        $("#set_address_diqu").val($(this).parents(".item_address").find(".item_address_body>p").text().match(/(.+)-(.+)-(.+)\s(.*)/)[2]);
        $("#set_address_scope").val($(this).parents(".item_address").find(".item_address_body>p").text().match(/(.+)-(.+)-([^\s]+)\s(.*)/)[3]);

        $("#set_address").show();
    });
    $("#select_address").on("click", ".select_address", function () {
        if(houtai.linshi_address_type===1){
            $("#address_text p").text($(this).parents(".item_address").find(".item_address_body>p").text());
            $("#address_text").show();
            houtai.linshi_peidan_order.address_id = $(this).parents(".item_address").find(".item_address_body>b").text();
        }else if(houtai.linshi_address_type===2){
            $("#receipt_address_text").text($(this).parents(".item_address").find(".item_address_body>p").text());
            $("#receipt_address_text").show();
            houtai.linshi_peidan_order.receipt_address_id= $(this).parents(".item_address").find(".item_address_body>b").text();
        }

        $("#select_address").hide();
    });


    /* 选择优惠券*/
    $("#close_select_coupon").bind("click", function () {
        $("#select_coupon").hide();
    });
    $("#select_coupon").on("click", ".select_coupon", function () {
        if($(this).prop("checked")){
            houtai.linshi_coupon.sign=1;
        }else{
            houtai.linshi_coupon.sign=0;
        }
         houtai.linshi_coupon.moeny=$(this).parents(".item_address").find(".coupon_money").text();
         houtai.linshi_coupon.id=$(this).parents(".item_address").find(".coupon_id").text();
        houtai.select_coupon_ajax();
    });


    /* 添加地址*/
    $("#close_add_address").bind("click", function () {
        $("#add_address").hide();
    });
    $("#address_add_config").bind("click", function () {
        if (houtai.jiancha()) {
            if (houtai.click_yes()) {
                houtai.address_add_ajax();
            }

        }

    });
    $("#address_city").bind("change", function () {
        // var address_data=houtai.linshi_peidan_address.address_data;
        // $.each(address_data,function (i,o) {
        //     if(o.province===$("#address_city").find(":selected").text()){
        //         var diqu_string="";
        //         var scope_string="";
        //         $.each(o.region,function () {
        //             diqu_string+='<option value="'+o.city+'">'+o.city+'</option>'
        //         });
        //         $.each(o.region[0].county,function (i) {
        //             scope_string+='<option value="'+o.region[0].county[i]+'">'+o.region[0].county[i]+'</option>'
        //         });
        //         $("#address_diqu").empty().append(diqu_string);
        //         $("#address_scope").empty().append(scope_string);
        //     }
        // });
    });
    $("#address_diqu").bind("change", function () {
        var address_data = houtai.linshi_peidan_address.address_data;
        $.each(address_data, function (i, o) {
            if (o.province === $("#address_city").find(":selected").text()) {
                $.each(o.region, function (i, o) {
                    if ($("#address_diqu").find(":selected").text() === o.city) {
                        var scope_string = "";
                        $.each(o.county, function (i) {
                            scope_string += '<option value="' + o.county[i] + '">' + o.county[i] + '</option>';
                        });
                        $("#address_scope").empty().append(scope_string);
                        return false;
                    }
                });
                return false;
            }
        });
    });

    /* 修改地址*/
    $("#close_set_address").bind("click", function () {
        $("#set_address").hide();
    });
    $("#address_set_config").bind("click", function () {
        if (houtai.jiancha()) {
            if (houtai.click_yes()) {
                houtai.address_set_ajax();
            }

        }
    });
    $("#set_address_city").bind("change", function () {
        // var address_data=houtai.linshi_peidan_address.address_data;
        // $.each(address_data,function (i,o) {
        //     if(o.province===$("#address_city").find(":selected").text()){
        //         var diqu_string="";
        //         var scope_string="";
        //         $.each(o.region,function () {
        //             diqu_string+='<option value="'+o.city+'">'+o.city+'</option>'
        //         });
        //         $.each(o.region[0].county,function (i) {
        //             scope_string+='<option value="'+o.region[0].county[i]+'">'+o.region[0].county[i]+'</option>'
        //         });
        //         $("#address_diqu").empty().append(diqu_string);
        //         $("#address_scope").empty().append(scope_string);
        //     }
        // });
    });
    $("#set_address_diqu").bind("change", function () {
        var address_data = houtai.linshi_peidan_address.address_data;
        $.each(address_data, function (i, o) {
            if (o.province === $("#set_address_city").find(":selected").text()) {
                $.each(o.region, function (i, o) {
                    if ($("#set_address_diqu").find(":selected").text() === o.city) {
                        var scope_string = "";
                        $.each(o.county, function (i) {
                            scope_string += '<option value="' + o.county[i] + '">' + o.county[i] + '</option>';
                        });
                        $("#set_address_scope").empty().append(scope_string);
                        return false;
                    }
                });
                return false;
            }
        });
    });

   /*  添加发票资质*/
   $("#close_add_receipt").bind("click",function () {
       $("#add_receipt").hide();
   });
   $("#receipt_add_config").bind("click",function () {
       if(houtai.jiancha()){
           if(houtai.click_yes()){
               houtai.add_receipt_ajax();
           }
       }
   });
   /*  修改发票资质*/
   $("#close_set_receipt").bind("click",function () {
       $("#set_receipt").hide();
   });
   $("#receipt_set_config").bind("click",function () {
       if(houtai.jiancha()){
           if(houtai.click_yes()){
               houtai.set_receipt_ajax();
           }
       }
   });
// 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });

    //确认事件
    $("#confirm").bind("click", function () {
        switch (houtai.linshi_type) {
            case 1:
                if (houtai.click_yes()) {
                    houtai.peidan_cancle_ajax();
                }
                break;
            case 2:
                if (houtai.click_yes()) {
                    houtai.peidan_reset_ajax();
                }
                break;
            case 3:
                if (houtai.click_yes()) {
                    houtai.peidan_clear_ajax();
                }
                break;
            case 4:
                if (houtai.click_yes()) {
                    houtai.peidan_remove_ajax();
                }
                break;
            case 5:
                if (houtai.click_yes()) {
                    houtai.order_create_ajax();
                }
                break;
        }


    });
    //确认修改
    $("#set").bind("click", function () {
        if (houtai.jiancha()) {
            if (houtai.click_yes()) {
                houtai.xiugai();
            }

        }

    });
    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);

    });
    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});
